from langchain_core.tools import tool
from typing import Annotated

def get_parameter(input_string: str) -> str:
    # Step 1: Keep only the part after the last "=" or ":"
    if '=' in input_string or ':' in input_string:
        last_equal_idx = input_string.rfind('=')
        last_colon_idx = input_string.rfind(':')
        # Get the substring after the last "=" or ":" whichever comes last
        if last_equal_idx > last_colon_idx:
            input_string = input_string[last_equal_idx + 1:]
        else:
            input_string = input_string[last_colon_idx + 1:]

    # Step 2: Extract the substring inside the last pair of quotes
    if '"' in input_string or "'" in input_string:
        last_double_quote_idx = input_string.rfind('"')
        last_single_quote_idx = input_string.rfind("'")
        if last_double_quote_idx > last_single_quote_idx:
            # Extract text between the last pair of double quotes
            input_string = input_string[:last_double_quote_idx].split('"')[-1]
        else:
            # Extract text between the last pair of single quotes
            input_string = input_string[:last_single_quote_idx].split("'")[-1]

    # Step 3: Strip leading and trailing whitespaces, tabs, and newlines
    input_string = input_string.strip()

    # Return the processed string
    return input_string


# 取得当前日期
from datetime import datetime
@tool
def get_current_date(input:str = "today")->str:
    """
    取得当天日期(今天日期)，以YYYY年MM月DD日的形式返回
    args:
        任意字符串
    Returns:
        以YYYY年MM月DD日的形式返回的当前日期
    """
    # 获取当前日期
    now = datetime.now()
    # 按照 "2023年09月04日" 格式进行格式化
    formatted_date = now.strftime("%Y年%m月%d日")
    return formatted_date

if __name__ == '__main__':
    test_input_v2 = """ key1: value1 = ' some text inside ' other content 'final text'
     ABC """
    processed_string_v2 = get_parameter(test_input_v2)
    print(processed_string_v2)